'''###########################################################################################################################
### File: create_main_character.py
### Name: Patrick Delaney, Andrew Oberlin
### Class: CSE 487
### Instructor: Dr.Zmuda
### Assignment: Final Project
### Description: Takes in the general information for teh main character
################ 
'''
from panda3d.core import Vec2, Point3, Vec3, Vec4
from direct.gui.DirectGui import *
from pandac.PandaModules import TextNode
from menu_button import MenuButton
from Tkinter import Tk
from tkFileDialog import askopenfilename
from panda3d.core import Filename

class GeneralInformationGUI:
	'''
	### Name: __init__ (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Starts the process of creating a main character
	'''
	def __init__(self, parent, charGen, mainCharacter):
		self.parent = parent
		self.characterGen = charGen
		self.mainCharacter = mainCharacter
		self.textureFile = self.mainCharacter.getMainTexture()
		self.models = dict()
		self.labels = dict()
		self.__loadModels()
	
	'''
	### Name: __loadModels
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Loads the models for the GUI
	'''	
	def __loadModels(self):
		# Input for the name of the character
		self.models['name_input_dummy'] = self.parent.base.aspect2d.attachNewNode('name_input_dummy')
		self.models['name_input_dummy'].setPos(-1.25, 0, 0.5)
		
		self.labels['name_label_text'] = TextNode('name_label_text')
		self.labels['name_label_text'].setText("Character's Name")
		self.models['name_label'] = self.models['name_input_dummy'].attachNewNode(self.labels['name_label_text'])
		self.models['name_label'].setScale(Vec3(0.10,0.10,0.075))
		
		self.models['name_input'] = DirectEntry(initialText=self.mainCharacter.getName(), cursorKeys=True, scale=Vec3(0.10, 0.075, 0.075))
		self.models['name_input'].setPos(Vec3(0.85, 0, 0))
		self.models['name_input'].reparentTo(self.models['name_input_dummy'])
		
		# Input for the texture file for the main charcter (this is the default texture file)		
		self.models['texture_input_dummy'] = self.parent.base.aspect2d.attachNewNode('texture_input_dummy')
		self.models['texture_input_dummy'].setPos(-1.25, 0, 0.3)
		
		self.labels['texture_label_text'] = TextNode('texture_label_text')
		self.labels['texture_label_text'].setText("Main Texture")
		self.models['texture_label'] = self.models['texture_input_dummy'].attachNewNode(self.labels['texture_label_text'])
		self.models['texture_label'].setScale(Vec3(0.10,0.10,0.075))
		
		self.models['preview_button'] = MenuButton(
			self.parent, 'load', 'models/gui/game_maker/dialog_button', 
			self.preview, Vec3(1.20, 0, 0.025), isMovie = False, text="Select Texture", 
			textPos=Vec3(-0.35, 0, -0.025), scale=Vec3(0.75,1,0.15)
		)
		
		self.models['preview_button'].reparentTo(self.models['texture_input_dummy'])
		
		# Input for the HP of the character
		self.models['health_input_dummy'] = self.parent.base.aspect2d.attachNewNode('health_input_dummy')
		self.models['health_input_dummy'].setPos(-1.25, 0, 0.1)
		
		self.labels['health_label_text'] = TextNode('health_label_text')
		self.labels['health_label_text'].setText("HP")
		self.models['health_label'] = self.models['health_input_dummy'].attachNewNode(self.labels['health_label_text'])
		self.models['health_label'].setScale(Vec3(0.10,0.10,0.075))
		
		self.models['health_input'] = DirectEntry(initialText=self.mainCharacter.getHP(), cursorKeys=True, scale=Vec3(0.10, 0.075, 0.075))
		self.models['health_input'].setPos(Vec3(0.85, 0, 0))
		self.models['health_input'].reparentTo(self.models['health_input_dummy'])
		
		# Input for the initial attack strength of character
		self.models['attack_strength_input_dummy'] = self.parent.base.aspect2d.attachNewNode('attack_strength_input_dummy')
		self.models['attack_strength_input_dummy'].setPos(-1.25, 0, -0.1)
		
		self.labels['attack_strength_label_text'] = TextNode('attack_strength_label_text')
		self.labels['attack_strength_label_text'].setText("Attack Strength")
		self.models['attack_strength_label'] = self.models['attack_strength_input_dummy'].attachNewNode(self.labels['attack_strength_label_text'])
		self.models['attack_strength_label'].setScale(Vec3(0.10,0.10,0.075))
		
		self.models['attack_strength_input'] = DirectEntry(initialText=self.mainCharacter.getAttack(), cursorKeys=True, scale=Vec3(0.10, 0.075, 0.075))
		self.models['attack_strength_input'].setPos(Vec3(0.85, 0, 0))
		self.models['attack_strength_input'].reparentTo(self.models['attack_strength_input_dummy'])
		
		self.models['preview'] = self.parent.loader.loadModelCopy("models/plane")
		self.models['preview'].reparentTo(self.parent.base.aspect2d)
		self.models['preview'].setPos(0.1, 0, -0.60)
		self.models['preview'].setScale(0.75)
		if (self.textureFile):
			texture = self.parent.loader.loadTexture(self.textureFile)
			self.models['preview'].setTransparency(1)
			self.models['preview'].setTexture(texture)
		
		self.models['next_step'] = MenuButton(
			self.parent, 'next_step', 'models/gui/game_maker/dialog_button', 
			self.characterGen.nextStep, Vec3(0.8, 0, -0.80), isMovie = False, text="Next Step", 
			textPos=Vec3(-0.225, 0, -0.025), scale=Vec3(0.5,1,0.15)
		)
	
	'''
	### Name: hide
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: hides this GUI
	'''
	def hide(self):
		for key in self.models.keys():
			self.models[key].hide()
			
	'''
	### Name: show
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: show this GUI
	'''
	def show(self):
		for key in self.models.keys():
			self.models[key].show()
	
	'''
	### Name: preview
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: finds the texture file for the main character
					 and loads it into a preview
	'''	
	def preview(self):
		try:
			root=Tk()
			self.file_opt = options = {}
			options['defaultextension'] = '' # couldn't figure out how this works
			options['filetypes'] = [('.png Files', '.png')]
			options['parent'] = root
			options['title'] = 'Choose a texture!'
			self.textureFile = askopenfilename(**self.file_opt)
			self.textureFile = Filename.fromOsSpecific(self.textureFile).getFullpath()
			root.destroy()
			texture = self.parent.loader.loadTexture(self.textureFile)
			self.models['preview'].setTransparency(1)
			self.models['preview'].setTexture(texture)
		except IOError, e:
			print "Problem loading texture!"
			
	'''
	### Name: getAttackText
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: finds the text in the attack input box
	'''	
	def getAttackText(self):
		return self.models['attack_strength_input'].get(plain=True)
		
	'''
	### Name: getNameText
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: finds the text in the name input box
	'''	
	def getNameText(self):
		return self.models['name_input'].get(plain=True)
		
	'''
	### Name: getHPText
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: finds the text in the HP input box
	'''	
	def getHPText(self):
		return self.models['health_input'].get(plain=True)
		
	'''
	### Name: getTextureFile
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: returns the texture file that has been chosen
	'''	
	def getTextureFile(self):
		return self.textureFile